﻿using System;
using System.Linq;
using Microsoft.Practices.Unity;
using Infrastucture;
using Infrastucture.Navigation;
using NavigationModule.ViewModels;
using TimeSplicerModule.Security;

namespace NavigationModule.Services
{
    public interface ILeftSideNavigationService : IService<LeftSideNavigationViewModel>
    { }

    public class LeftSideNavigationService : Service, ILeftSideNavigationService
    {
        private ISession _session;

        public LeftSideNavigationService()
            : base()
        {
            _session = this.Container.Resolve<ISession>();
        }

        public LeftSideNavigationViewModel Get(NavigationMenu menu)
        {
            if (menu == NavigationMenu.Settings)
                return getSettingsModel();

            return new LeftSideNavigationViewModel();
        }

        private LeftSideNavigationViewModel getSettingsModel()
        {
            _session = this.Container.Resolve<ISession>();
            var model = new LeftSideNavigationViewModel();

            if (_session != null && _session.User != null)
            {
                // get permissions
                var developer = _session.ApplicationPermissions.FirstOrDefault(x => x.Permission == ApplicationPermission.Developer);
                var createDelete = _session.ApplicationPermissions.FirstOrDefault(x => x.Permission == ApplicationPermission.Create_Delete);

                // get items
                // if can see settings then teams, users, categories ok
                var settingsIndex = _session.ApplicationItems.FirstOrDefault(x => x.Item == ApplicationItem.Settings_Index);
                var projectRoleList = _session.ApplicationItems.FirstOrDefault(x => x.Item == ApplicationItem.Application_Items);
                var appRolesList = _session.ApplicationItems.FirstOrDefault(x => x.Item == ApplicationItem.Application_Roles);
                var statusList = _session.ApplicationItems.FirstOrDefault(x => x.Item == ApplicationItem.Statuses);
                var priorityList = _session.ApplicationItems.FirstOrDefault(x => x.Item == ApplicationItem.Priorities);
                var services = _session.ApplicationItems.FirstOrDefault(x => x.Item == ApplicationItem.Services);

                // get user permissions
                var settingsIndexPermission = _session.User.ApplicationRole.Permissions.FirstOrDefault(x => x.ApplicationItemGuid == settingsIndex.ApplicationItemGuid);
                var projectRoleListPermission = _session.User.ApplicationRole.Permissions.FirstOrDefault(x => x.ApplicationItemGuid == projectRoleList.ApplicationItemGuid);
                var appRolesLiustPermission = _session.User.ApplicationRole.Permissions.FirstOrDefault(x => x.ApplicationItemGuid == appRolesList.ApplicationItemGuid);
                var statusListPermission = _session.User.ApplicationRole.Permissions.FirstOrDefault(x => x.ApplicationItemGuid == statusList.ApplicationItemGuid);
                var priorityListPermission = _session.User.ApplicationRole.Permissions.FirstOrDefault(x => x.ApplicationItemGuid == priorityList.ApplicationItemGuid);
                var servicesPermission = _session.User.ApplicationRole.Permissions.FirstOrDefault(x => x.ApplicationItemGuid == services.ApplicationItemGuid);


                if (settingsIndexPermission.Permission.Hierarchy <= createDelete.Hierarchy)
                {
                    var users = new NavigationLink("Users", 0, RegionNames.LeftSideRegion, Module.TimeSplicer);
                    model.Links.Add(users);

                    var categories = new NavigationLink("Categories", 1, RegionNames.LeftSideRegion, Module.TimeSplicer);
                    model.Links.Add(categories);

                    var teams = new NavigationLink("Teams", 2, RegionNames.MainRegion, Module.TimeSplicer);
                    model.Links.Add(teams);
                }

                
                // don't need this for now?
                //var jobRoles = new NavigationLink("Job Roles", 1, RegionNames.MainRegion, Module.TimeSplicer);
                //model.Links.Add(jobRoles);





                // developer only
                if (projectRoleListPermission.Permission.Hierarchy == developer.Hierarchy)
                {
                    var projectRoles = new NavigationLink("Project Roles", 4, RegionNames.LeftSideRegion, Module.TimeSplicer);
                    model.Links.Add(projectRoles);
                }

                if (appRolesLiustPermission.Permission.Hierarchy == developer.Hierarchy)
                {
                    var appRoles = new NavigationLink("Application Roles", 5, RegionNames.MainRegion, Module.TimeSplicer);
                    model.Links.Add(appRoles);
                }

                if (statusListPermission.Permission.Hierarchy == developer.Hierarchy)
                {
                    var statuses = new NavigationLink("Statuses", 6, RegionNames.LeftSideRegion, Module.TimeSplicer);
                    model.Links.Add(statuses);
                }

                if (priorityListPermission.Permission.Hierarchy == developer.Hierarchy)
                {
                    var priorities = new NavigationLink("Priorities", 7, RegionNames.LeftSideRegion, Module.TimeSplicer);
                    model.Links.Add(priorities);
                }

                if (servicesPermission.Permission.Hierarchy == developer.Hierarchy)
                {
                    var servies = new NavigationLink("Services", 8, RegionNames.LeftSideRegion, Module.TimeSplicer);
                    model.Links.Add(servies);
                }

                // eventually expose items and permissions


                model.Links = model.Links.OrderBy(o => o.Order).ToList();
            }

            return model;
        }

        public LeftSideNavigationViewModel Get()
        {
            throw new NotImplementedException();
        }
    }
}
